const _ = require("../import");

/**
 * @api {post} /car/addCar  addCar
 * @apiDescription 详情页添加商品到购物车
 * @apiName addCar
 * @apiGroup car
 * @apiParam {number} waresid 需要被收藏的商品id，wares_id字段
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "成功添加购物车"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "不要重复收藏"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/car/addCar
 * @apiVersion 1.0.0
 */
_.router.post('/addCar', async function(req, res, next) {
  var shopid = req.body.waresid;

  var IsAddShopCar = await _.db.query({
    sql: _.config.sql.selectCarShop,
    data: [ _.jwt.decode(req.headers.token).id, shopid]
  });

  if(IsAddShopCar.length != 0) {
    res.json({
      status: 404,
      message: '不要重复收藏',
      result: null
    });
  } else {
    var data = await _.db.query({
      sql: _.config.sql.addCar,
      data: [ _.jwt.decode(req.headers.token).id, shopid, 1]
    });
    res.json({
      status: 200,
      message: '成功添加购物车',
      result: data
    });
  }
});


/**
 * @api {post} /car/getCarList  getCarList
 * @apiDescription 获取用户的购物车商品数据（不需要参数，传空对象 {} 即可 ）
 * @apiName getCarList
 * @apiGroup car
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "购物车没有数据"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/car/getCarList
 * @apiVersion 1.0.0
 */
_.router.post('/getCarList', async function(req, res, next) {
  var data = await _.db.query({
    sql: _.config.sql.getCarList,
    data: [ _.jwt.decode(req.headers.token).id]
  });

  if(data.length != 0) {
    res.json({
      status: 200,
      message: '请求成功',
      result: data
    });
  } else {
    res.json({
      status: 200,
      message: '购物车没有数据',
      result: {}
    });
  }

});



/**
 * @api {post} /car/checkCar  checkCar
 * @apiDescription 检查商品是否已经被用户添加到购物车，已添加 result 为true，未添加 result 为false
 * @apiName checkCar
 * @apiGroup car
 * @apiParam {number} waresid 商品id，wares_id字段
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "商品已添加购物车"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 200,
 *   "message" : "商品未添加购物车"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/car/checkCar
 * @apiVersion 1.0.0
 */
_.router.post('/checkCar', async function(req, res, next) {
  var shopid = req.body.waresid;

  var IsAddShopCar = await _.db.query({
    sql: _.config.sql.selectCarShop,
    data: [ _.jwt.decode(req.headers.token).id, shopid]
  });

  if(IsAddShopCar.length != 0) {
    res.json({
      status: 200,
      message: '商品已添加购物车',
      result: {
        isAddCar: true
      }
    });
  } else {
    res.json({
      status: 200,
      message: '商品未添加购物车',
      result: {
        isAddCar: false
      }
    });
  }

});


/**
 * @api {post} /car/deleteCar  deleteCar
 * @apiDescription 删除被添加到购物车的商品
 * @apiName deleteCar
 * @apiGroup car
 * @apiParam {Array} carid 购物车商品id，从getCarList接口中获得car_id即可。参数格式："[1,2,3]"
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "删除成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 200,
 *   "message" : "删除失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/car/deleteCar
 * @apiVersion 1.0.0
 */
_.router.post('/deleteCar', async function(req, res, next) {
  var carid = JSON.parse(req.body.carid);
  console.log("carid: ", carid)

  carid.forEach(async function(val) {
    var deleteCar = await _.db.query({
      sql: _.config.sql.deleteCarShop,
      data: [ val ]
    });
  });

  res.json({
    status: 200,
    message: '删除成功',
    result: {}
  });
});


/**
 * @api {post} /car/updateCarNum  updateCarNum
 * @apiDescription 更新购物车需要购买的商品数量
 * @apiName updateCarNum
 * @apiGroup car
 * @apiParam {number} carid 购物车商品id，从getCarList接口中获得car_id即可
 * @apiParam {number} quantity 购物车商品的数量
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "更新成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 200,
 *   "message" : "更新失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/car/updateCarNum
 * @apiVersion 1.0.0
 */
_.router.post('/updateCarNum', async function(req, res, next) {
  var carid = req.body.carid;
  var quantity = req.body.quantity;

  var updateCarNum = await _.db.query({
    sql: _.config.sql.updateCarNum,
    data: [ quantity, carid ]
  });

  console.log(updateCarNum)
  if(updateCarNum.affectedRows == 1) {
    res.json({
      status: 200,
      message: '更新成功',
      result: {}
    });
  } else {
    res.json({
      status: 404,
      message: '更新失败',
      result: {}
    });
  }
});


/**
 * @api {post} /car/submitCar  submitCar
 * @apiDescription 更新购物车需要购买的商品数量，提交的数据是个对象，例如："{"carList":[{"waresid":113,"goodsnum":2,"goodsprice":1000},{"waresid":114,"goodsnum":1,"goodsprice":1100}]}"
 * @apiName submitCar
 * @apiGroup car
 * @apiParam {Object} carList 为对象，包含下面三个参数，需要前端自己构造数组
 * @apiParam {string} waresid 商品的waresid
 * @apiParam {string} goodsnum 购买的数量
 * @apiParam {string} goodsprice 购买的单价
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "订单增加成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 200,
 *   "message" : "订单增加失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/car/submitCar
 * @apiVersion 1.0.0
 */
_.router.post('/submitCar', async function(req, res, next) {
  //"{"carList":[{"waresid":113,"goodsnum":2,"goodsprice":1000},{"waresid":114,"goodsnum":1,"goodsprice":1100}]}"
  var carList = JSON.parse(req.body.carList);

  carList.forEach(async function(val) {
    val.user_id = _.jwt.decode(req.headers.token).id;

    var addCarToOrder = await _.db.query({
      sql: _.config.sql.addCarToOrder,
      data: [ val.user_id,val.waresid,val.goodsnum,val.goodsprice,val.goodsnum * val.goodsprice]
    });
  });

  res.json({
    status: 200,
    message: '订单增加成功',
    result: {}
  });
});
module.exports = _.router;
